import gameobject
from gameobject import *

import random

import pygame
from pygame import *

import joshfunctions
from joshfunctions import *

class Item(GameObject):
    def __init__(self, position, spritesheet):
        GameObject.__init__(self)
        self.frames = [0,0,0,0]
        self.scaledframes = [0,0,0,0]
        spritepos = (random.randrange(0,12),random.randrange(0,6))
        self.speed = .3
        self.animationcountdown = self.speed
        self.blinkingcountdown = self.speed / 3.0
        self.blinkingonoff = True
        self.blinking = False
        self.warping = True
        self.warpcountdown = 1+.5*random.random()
    
        for j in range(4):
            self.frames[j] = Surface((8,8))
            self.frames[j].set_colorkey((0,255,255))
            self.frames[j].blit(spritesheet, (0,0), ((spritepos[0]*8),(spritepos[1]*8),8,8))
        for x in range(self.frames[0].get_width()):
            for y in range(self.frames[0].get_height()):
                currentcolor = self.frames[0].get_at((x,y))

                if currentcolor == (0,0,0, 255):
                    self.frames[0].set_at((x,y),(196,207,161))
                    self.frames[1].set_at((x,y),(139,149,109))
                    self.frames[2].set_at((x,y),(74,81,57))
                    self.frames[3].set_at((x,y),(31,31,31,255))

        self.position = position
        #self.position = (random.randrange(0,1024), random,randrange(506)+556)
        self.rect = Rect(0.0,0.0,8.0,8.0)
        self.rect.center = self.position
        self.destroyed = False
        
        for j in range(4):
            self.scaledframes[j] = transform.scale(self.frames[j], (self.frames[j].get_width()*4,self.frames[j].get_height()*4))
                
        self.rect.center = self.position
        self.drawrect = self.rect.copy().inflate(32.0, 32.0)
        self.drawrect.center = self.position
        self.drawrect.left += 512
        self.drawrect.top += 288

        print "Item created at:", self.position

    def update(self, etime,players):

        if self.warping:
            self.animationcountdown -= etime
            if self.animationcountdown <= 0.0:
                self.animationcountdown = self.speed*.5 * (1.0 + random.random() / 4)

            self.warpcountdown -= etime
            if self.warpcountdown <= 0.0:
                self.warping = False
                self.blinkingonoff = False
                self.blinking = False
                #print "no more blinking"

        if self.blinkingonoff:
            self.blinkingcountdown -= etime
            if self.blinkingcountdown <= 0.0:
                self.blinking = True if self.blinking == False else False
                self.blinkingcountdown = .1*(2.0+random.random())
        else:
            self.blinking = False
            self.blinkingcountdown = 0.0
            for i in range(len(players)):
                position = (players[i].position[0]+16,players[i].position[1]+16)

                #print self.position, position
                if dist(self.position, position) <= 32:
                    
                    #print "Item eaten by Player ", i+1
                    self.destroyed = True
                    players[i].getitem()
                    players[i].score += int((10+random.randint(0,10))*players[i].combo)
                    players[i].combo+= .1
                    players[i].itemscollected.append(self.frames[3])

        

        currentx = self.position[0]
        currenty = self.position[1]



        


    def drawbottom(self, surf,night):
        if self.blinking:
            return
        if night:
            shadowcolor = 1
        else:
            shadowcolor = 1
        surf.blit(self.scaledframes[shadowcolor], (self.drawrect.left+6.0,self.drawrect.top+(6.0)))

    def drawtop(self, surf,night):
        if self.blinking:
            return
        if night:
            spritecolor = 0
        else:
            spritecolor = 3
        surf.blit(self.scaledframes[spritecolor], (self.drawrect.left+4,self.drawrect.top+4))
